package com.wzl.customviewproject.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 作者：longxiansen
 * 功能：仿小米商城分类页面
 * 左侧子view平分屏幕高度
 */
public class MiTableLayout extends ViewGroup {
    public MiTableLayout(Context context) {
        super(context);
    }

    public MiTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MiTableLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int selectedPosition = 0;//选中的位置
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int itemHeight = heightSize/childCount;
        int maxWidth = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
//            int measuredWidth = childView.getMeasuredWidth();
// 每一行的测量宽度,这里的一个错误是还未执行childView.measure方法就去获取宽度，这样获取到的为0
            LayoutParams childViewLayoutParams = childView.getLayoutParams();
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            maxWidth = Math.max(maxWidth,widthSize);
            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, getPaddingLeft() + getPaddingRight(), widthSize);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, getPaddingTop() + getPaddingBottom(), itemHeight);
            childView.measure(childWidthMeasureSpec,childHeightMeasureSpec);

            if (i==selectedPosition) {
                ((TextView)childView).setTextSize(16);
                ((TextView)childView).getPaint().setFakeBoldText(true);
                ((TextView)childView).setTextColor(Color.BLACK);
            }else {
                ((TextView)childView).setTextSize(14);
                ((TextView)childView).getPaint().setFakeBoldText(false);
                ((TextView)childView).setTextColor(Color.parseColor("#656565"));
            }
        }
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int realWidth = (widthMode == MeasureSpec.EXACTLY)?widthSize:maxWidth;
        int realHeight = (heightMode == MeasureSpec.EXACTLY)?heightSize:itemHeight;
        setMeasuredDimension(realWidth,realHeight);
    }

    private int curT = 0;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int right = childView.getMeasuredWidth();
            int top = curT;
            //这里要用childView.getMeasureHeight而不是getMeasuredHeight()。后者获取的是整个布局的高度
            childView.layout(left,top,right,top+ childView.getMeasuredHeight());
            curT += childView.getMeasuredHeight();
        }
    }

    public void setCurrentItem(int position){
        selectedPosition = position;
        postInvalidate();
/*        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            TextView tv = (TextView) getChildAt(i);
            tv.setTextSize(i == position ? 16:14);
            tv.setTextColor(i == position ? Color.BLACK : Color.parseColor("#656565"));
        }*/
    }
}
